package b4.view.component.pack
{
	import b4.config.BasicObjConfig;
	import b4.config.Config;
	import b4.config.EquipConfig;
	import b4.config.ObjConfig;
	import b4.model.vo.player.BasicObj;
	import b4.model.vo.player.Equip;
	import b4.model.vo.player.EquipProperty;
	import b4.model.vo.player.Obj;
	import b4.model.vo.player.Pack;
	import b4.view.component.events.ItemClickEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import org.aswing.*;
	import org.aswing.border.LineBorder;

	public class PackageDialog extends JFrame
	{
		private var packageItems:Array;
		public var filterPane:JPanel;
		
		public function PackageDialog(owner:Object)
		{
			super(owner, "包裹");
			setResizable(false);//设置frame是否可以放大缩小；
			var wholePane:JPanel = new JPanel();
			
			var tabbedPane:JTabbedPane = new JTabbedPane();
			packageItems = new Array();
			for (var j:int = 0; j < 6; j++)
			{
				var pane:JPanel = new JPanel(new FlowLayout());
				pane.setPreferredHeight(7*45);
				pane.setPreferredWidth(6*46);
				pane.setBorder(new LineBorder(null, ASColor.RED));
				for (var i:int = 0; i < 42; i++)
				{
					var packageItem:PackageItem = new PackageItem();
					packageItem.x = (i % 6) * 45;
					packageItem.y = int(i / 6) * 45;
					packageItems.push(packageItem);
					pane.append(packageItem);
				}
				tabbedPane.appendTab(pane, (j + 1).toString());
			}

			filterPane = packageMenu();
			filterPane.setPreferredWidth(45 * 7);
			filterPane.setBorder(new LineBorder(null, ASColor.BLUE));
			wholePane.append(tabbedPane);
			
			filterPane.setPreferredHeight(30);
			wholePane.append(filterPane);
			this.setContentPane(wholePane);
			this.setComBoundsXYWH(100, 100, 45*8, 10*46);
		}

		
		private function packageMenu():JPanel
		{
			var bar:JPanel = new JPanel();
			var listData :Array = new Array();
			listData.push("所有物品");
			for (var i:int = 1; i < BasicObjConfig.TYPE_NAMES.length+1; i++)
			{
				listData.push(BasicObjConfig.TYPE_NAMES[i-1]);
			}
			var model : VectorListModel = new VectorListModel(listData)
			var selComb:JComboBox = new JComboBox(model);
			selComb.setPreferredWidth(150);
			selComb.setMaximumRowCount(5);
			selComb.setSelectedIndex(0);
			selComb.addSelectionListener(onMenuItemClick);
			bar.append(selComb);
			return bar;
		}
		
		public function onMenuItemClick(e:Event):void
		{
			var comb :JComboBox = e.currentTarget as JComboBox;
			var event :ItemClickEvent = new ItemClickEvent(ItemClickEvent.MENU_ITEM_CLICK);
			event.data = comb.getSelectedIndex();
			dispatchEvent(event);
		}
		
		public function onMouseClick(e:MouseEvent):void
		{
			var objItem:BasicObjItem = e.currentTarget as BasicObjItem;
			var event:ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_CLICK);
			event.data = objItem.obj;
			dispatchEvent(event);
		}
		

		public  function onMouseOver(e:MouseEvent):void
		{
			var objItem:BasicObjItem = e.currentTarget as BasicObjItem;
			var objTip:Sprite = getObjSprite(objItem.obj);
			var globalPoint:Point = objItem.parent.localToGlobal(new Point(objItem.x, objItem.y));
			objTip.x = globalPoint.x + objItem.width;
			objTip.y = globalPoint.y + objItem.height;
			var event:ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_MOUSE_OVER);
			event.data = objTip;
			dispatchEvent(event);
		}
		
		public  function onMouseOut(e:MouseEvent):void
		{
			var event:ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_MOUSE_OUT);
			dispatchEvent(event);
		}
		
		public function refreshPackage(pack:Pack, filterType:int = 0):void
		{
			for (var indexStr:String in  packageItems)
			{
				var pItem:PackageItem = packageItems[indexStr] as PackageItem;
				pItem.unloadAsset();		
			}
			var count:int = 0;
			for (var objIndexStr:String in pack.objList)
			{
				var obj:Obj = pack.objList[objIndexStr];
				var realObj:Obj = new Obj();
				realObj.getObjCopy(obj);
				
				if (pack.smithyObjList.hasOwnProperty(obj.index))
				{
					var smithyObj:Obj = pack.smithyObjList[obj.index] as Obj;
					realObj.num -= smithyObj.num
				}
				var objItem:ObjItem = new ObjItem(realObj);
				var objConfig:ObjConfig = Config.objConfigs[obj.index]; 
				var objType:int = objConfig.type;
				if ((filterType == 0 || objType == filterType - 1) && realObj.num >= 1)
				{
					var packageItem:PackageItem = packageItems[count++];
					packageItem.loadItem(objItem);
					objItem.addEventListener(MouseEvent.CLICK, onMouseClick);
					objItem.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					objItem.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				}
			}
			for (var equipIndexStr:String in pack.equipList)
			{
				var equipObj:Equip = pack.equipList[equipIndexStr];
				if(!pack.smithyEquipList.hasOwnProperty(equipObj.id))
				{
					var equipItem:EquipItem = new EquipItem(equipObj);
					if (filterType == 0)
					{
						packageItem = packageItems[count++];
						packageItem.loadItem(equipItem);
						equipItem.addEventListener(MouseEvent.CLICK, onMouseClick);
						equipItem.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
						equipItem.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
					}
				}
	
			}			
		}
		
		public static function getObjSprite(obj:BasicObj):Sprite
		{
			var objTip:ObjMessageTip = new ObjMessageTip(250);
			if (obj.index < 10000)
			{
				var equipConfig:EquipConfig = Config.objConfigs[obj.index]; 
				var equip:Equip = obj as Equip;
				if (equip.strengthen > 0)
					objTip.appendText("<font size='16'>" + equipConfig.name + "</font> (+" + equip.strengthen + ")");
				else
					objTip.appendText("<font size='16'>" + equipConfig.name + "</font>");
//				objTip.appendText("分类： " + equipConfig);
				objTip.appendText("职业： " + EquipConfig.OCCUPATION_NAMES[equipConfig.occupation]);
				objTip.appendText("品质： " + EquipConfig.QUALITY_NAMES[equip.quality]);
				for (var i:int = 0; i < equip.equipPropertyList.length; i++)
				{
					var equipProperty:EquipProperty = equip.equipPropertyList[i];
					objTip.appendText("<font color='#00AA00'>" + EquipConfig.EQUIP_PROPERTY_NAMES[equipProperty.propertyIndex] + " " + 
					(equipProperty.propertyNum < 0 ? "" : "+") + equipProperty.propertyNum + 
					"      （洗炼" + equipProperty.level + "星）" + "</font>");
				}
			}
			else
			{
				var objConfig:ObjConfig = Config.objConfigs[obj.index]; 
				objTip.appendText("<font size='16'>" + objConfig.name + "</font>");
				objTip.appendText("分类： <font color='#" + BasicObjConfig.TYPE_COLORS[objConfig.type] + "'>" + BasicObjConfig.TYPE_NAMES[objConfig.type] + "</font>");
				objTip.appendText(objConfig.description);
				objTip.appendText("<font color='#AAAAAA'>商店售价： " + objConfig.salePrice + "</font>");
			}
			var rrect:Sprite = new Sprite();
			rrect.graphics.lineStyle(2, 0x888888);
			rrect.graphics.beginFill(0x0000000, 0.8);
			rrect.graphics.drawRoundRect(0, 0, objTip.width + 10, objTip.height + 10, 15, 15);
			rrect.graphics.endFill();
			objTip.x = 5;
			objTip.y = 5;
			rrect.addChild(objTip);
			return rrect;
		}
	}
}